// packageShop/pages/nearbyStores/nearbyStores.js
var util = require('../../../utils/util')   //引入配置公共请求接口
var configUrl = require('../../../config/config')   //引入配置接口文件
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    footprintGuid: '', //足迹全局id
    CustomBar: app.globalData.CustomBar,
    shareUserId:'',
    addressList:[],
    title:'附近门店',
    statusHeight:app.globalData.StatusBar,
  },
  /**
   * 生命周期函数--监听页面加载
   */
 async onLoad(options) {
   // 如果点击推荐门店进进入页面，则显示导购下面所属推荐门店
   let shareUserId = app.globalData.shareUserId||''
    this.setData({
      shareUserId:shareUserId
    })
    if(options.isLink||options.goPath||options.detail=='nearBy'){
      //如果为模块附近门店进入 则按照距离显示门店
      console.log('模块附近门店进入')
      this.getNearbyShop(options)
    }else if(shareUserId||options.detail=='share'){
      // 分享进入 分享人所属门店
      wx.setNavigationBarTitle({
        title: '推荐门店'
      })

      let dataList =await this.getShareShops(shareUserId)
      if(dataList.length>0) {
        this.setData({
          shopsAddr:dataList,
          title:'推荐门店'
        })
      }else{
        this.getNearbyShop(options)
      }
     
    }else{
      // 否则按距离显示附件门店
      this.getNearbyShop(options)
    }
  
    
  },
  
 getShareShops(user){
  return new Promise((resolve,reject)=>{
    app.reqFun.requestJson('get',configUrl.recommendShop+'/'+user,{},(res)=>{
      if(res.data.success==1&&res.data.data.length>0){
        resolve (res.data.data)
      }else{
        resolve ([])
      }
    })
})
 },
  getNearbyShop(options){
      wx.setNavigationBarTitle({
        title: '附近门店'
      })
      this.setData({title:'附近门店'})
      if(options.goPath=='info'){
        var data= wx.getStorageSync('looknearbyStores')
        this.getgeocode(data.data)
        this.setData({
          postData:data.data,
          showdata:data.showdata
        })
      }else{   //直接进入附近门店
        var address=wx.getStorageSync('addrCode')
        var showData=address.province+address.city+address.district+address.townName
        this.getgeocode(showData+address.street+address.streetNumber)
        this.setData({
          postData:showData+address.street+address.streetNumber,
          showdata:showData
        })
     }
  },
  getgeocode(data){   //地址获取经纬度
    var that=this;
    var parms={address:data}
    app.reqFun.requestJson('get',configUrl.geocode,parms,(res)=>{
      if(res.data.success==1){
        that.nearbyShop(res.data.data.latitude,res.data.data.longitude)
      }
    })
  },
  nearbyShop(lat,long){  //经纬度获取附近门店   distance范围 latitude纬度   longitude 经度
    var that=this;
    var parms={
      distance:100000,
      latitude:lat,
      longitude:long
    }
    app.reqFun.requestJson('get',configUrl.nearbyShop,parms,(res)=>{
      if(res.data.success==1){
        if(res.data.data){
          that.setData({
            shopsAddr:res.data.data
          })
        }else{
          that.setData({
            shopsAddr:[]
          })
        }
      }else{
        wx.showToast({
          title: '获取门店信息失败：'+res.data.msg,
          icon:'none'
        })
        that.setData({
          shopsAddr:[]
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      footprintGuid: util.guid()
    })
    //保存访问足迹
    app.saveAccessFootprint(this.data.footprintGuid, 'nearbyStore', null)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      success: (res) => {
        console.log('刷新成功')
      },
    }) 
  },
  async refresh(){
    var that=this;
    var data=this.data.postData
    await app.getUserLocation()
    await that.getgeocode(data)
    wx.stopPullDownRefresh({
      success: (res) => {
        console.log('刷新成功')
      },
    }) 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  goreBack:function(e){
    wx.removeStorageSync('looknearbyStores')
    wx.navigateBack({
      delta: 1
    })
  },
})